<?php
namespace Common\Taglib;
using \Think\Template\TagLib;
/**
 * TagLibDbhelper
 *
 * @author Alen
 * @since 1.0 2014-7-4 by sutroon
 * @example
 * 1.需要在配置文件config.php中增加：
 * 'TAGLIB_LOAD' => true, //加载标签库打开
 * 'APP_AUTOLOAD_PATH' => '@.TagLib',
 * 'TAGLIB_BUILD_IN' => 'Cx,dbhelper', // Cx为核心标签库名称,必需填写
 */
class TagLibDbhelper extends TagLibBase {

    protected $tags = array(
        'field' => array('attr' => 'where,id,name', 'close' => 0),
        'list' => array('attr' => 'attrs', 'close' => 1),
        'treeset' => array('attr' => 'attrs', 'close' => 1),
        'treeselect' => array('attr' => 'attrs', 'close' => 0),
        'treelist' => array('attr' => 'attrs', 'close' => 0),
        'catalogselect' => array('attr' => 'attrs', 'close' => 0),
    );

    /**
     * 执行查询并返回结果集
     * @param $attr
     * @param $content
     * @return string
     * @since 1.0 2014-10-30 by sutroon
     * @example
     *      <taglib name="dbhelper"/>
     *      <dbhelper:list table="tuser_member" limit="10" where_static="userType='ADMIN'">
     *          <a href="{:U('User/item_edit','id='.$id)}">2:{$userName}</a>,
     *      </dbhelper:list>
     *      <dbhelper:list table="tuser_member" where_static="userType='ADMIN'" paging="true" pagesize="25">
     *          <a href="{:U('User/item_edit','id='.$id)}">2:{$userName}</a>,
     *      </dbhelper:list>
     */
    public function _list($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'list');
        return $this->base_list_script($attr, $content);
    }

    /** 获取某字段的值
     * @param $attr
     * @param $content
     * @return string
     * @since 1.0 2014-10-30 by sutroon; 2.0 2014-11-3 by sutroon 由value重构为field
     * @example
     *  <taglib name="dbhelper"/>
     *  <dbhelper:field name="id" />
     */
    public function _field($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'field');
        return $this->base_get_tpl_data($attr['name'], true);
    }

    /**
     * 获取树形结果集
     * @param $attr
     * @param $content
     * @since 1.0 2014-11-1 by sutroon
     * @example
     * <taglib name="dbhelper"/>
     * <ul>
     *   <dbhelper:treeset table="tgeneral_catalog" orderby="id">
     *      <li><a href="{:U('Catalog/item_edit','id='.$id)}">{$catalogName}</a>
     *          <ul>
     *              <foreach name="sub" item="vo">
     *                  <li><a href="{:U('Catalog/item_edit','id='.$vo['id'])}">----{$vo.catalogName}</a>
     *                      <ul>
     *                          <foreach name="vo.sub" item="vo3">
     *                              <li><a href="{:U('Catalog/item_edit','id='.$vo3['id'])}">--------{$vo3.catalogName}</a>
     *                                  <ul>
     *                                      <foreach name="vo3.sub" item="vo4">
     *                                          <li><a href="{:U('Catalog/item_edit','id='.$vo4['id'])}">------------{$vo4.catalogName}</a></li>
     *                                      </foreach>
     *                                  </ul>
     *                              </li>
     *                          </foreach>
     *                      </ul>
     *                  </li>
     *              </foreach>
     *          </ul>
     *      </li>
     *   </dbhelper:treeset>
     * </ul>
     */
    public function _treeset($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'treeset');
        return $this->base_treeset_script($attr, $content);
    }

    /**
     * 获取树形Select控件
     * @param $attr
     * @param $content
     * @return string
     * @since 1.0 2014-11-3 by sutroon
     * @example
     *  <taglib name="dbhelper"/>
     *  <dbhelper:treeselect table="tgeneral_catalog" orderby="id" value="3" />
     */
    public function _treeselect($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'treeselect');
        return $this->base_treeselect($attr);
    }

    /**
     * 获取分类下拉菜单
     * @param type $attr
     * @param type $content
     * @return type
     * @since 1.0 2014-12-18 by sutroon
     */
    public function _catalogselect($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'catalogselect');
        $siteid = $this->base_get_tpl_data($attr['siteid']);        
        $extags = $attr['extags'];
        $attr['table'] = 'tgeneral_catalog';
        $attr['where'] = "siteID=$siteid and exTags='$extags' and parentID={0}";
        return $this->base_treeselect($attr);
    }

    /**
     * 树形控件
     * @param $attr
     * @param $content
     * @return string
     * @since 1.0 2014-11-5 by sutroon 由AdminAction.itemTreeList()(2014-10-28)迁移过来
     * @example
     *  <taglib name="dbhelper"/>
     *  <dbhelper:treelist table="tuser_permission" checkbox="true" values="{$values}" />
     *  $this->assign('values', M('tuser_member')->where('id=' . $uid)->getField('permissionRule'));
     */
    function _treelist($attr, $content) {
        $attr = $this->parseXmlAttr($attr, 'treelist');
        return $this->base_treelist($attr);
    }

}
